package mail.impl;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

import mail.bean.EmailUser;
import mail.base.FileProcessor;

public class TextFileProcessor implements FileProcessor {
  
  final public static String SPLIT = " ";

	public ArrayList<EmailUser> process(File file) {
		if(file == null || !file.exists() || !file.getName().toUpperCase().endsWith("TXT")) {
			return null;
		}
		ArrayList<EmailUser> userList;
		try {
			BufferedReader r = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
			userList = new ArrayList<EmailUser>();
			String tmp = null;
			String[] array = null;
			EmailUser user = null;
			while((tmp = r.readLine()) != null) {
			  System.out.println(tmp);
				if(tmp.trim().length() > 0) {
					array = tmp.split(SPLIT);
					System.out.println(array.length+":" + array[0] + ":" + array[1]);
					if(array.length >= 2) {
						user = new EmailUser();
						user.setName(array[0]);
						user.setEmailAddr(array[1]);
						userList.add(user);
					}
				}
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return null;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
		return userList;
	}

}
